var prefix = jsctx + "/acctTenantOperator";

var acctBillOrder = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
        setTimeout(function () {
            $("thead tr").css("height","55px");
            $(".fixed-table-header-columns1").css("width",'189px');
            $(".fixed-table-body-columns1").css("width",'189px');
        },400)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        // $(".fixed-table-header-columns").remove()
        // $(".fixed-table-body-columns").remove()
        // registCheckbox($('#exampleTable'))
        // $('#exampleTable').bootstrapTable('refresh')
        setTimeout(function () {
            $("thead tr").css("height","55px");
            $(".fixed-table-header-columns1").css("width",'189px');
            $(".fixed-table-body-columns1").css("width",'189px');
        },10)
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;


    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageAcctBillOrder",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 30,
                    pageList: [10,20,30],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    // leftFixedColumns:true,
                    rightFixedColumns: true,
                    // leftFixedNumber: 5,
                    rightFixedNumber: 1,
                    showColumns: false,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            tenantOperatorCode: $('#tenantOperatorCode').val(),
                            billOrderNum: $('#billOrderNum').val(),
                            elecStationId: $('#elecStationId').val(),
                            status: $('#status').val(),
                            startTime: $('#startTime').val(),
                            endTime: $('#endTime').val(),
                            //运营商为0,分销商为1
                            userType: 0,
                        };
                    },
                    columns: [
                        {
                            checkbox: true, field: '', width: '50', align: 'left',
                            formatter:function (value,row) {
                                //如果已经操作禁止选择。具体可以根据业务
                                if(row.acctRuleId){
                                    //不进行禁用
                                    return { disabled : false}
                                }else{
                                    //禁用
                                    return { disabled : true}
                                }
                            }
                        },
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'billOrderNum', title: '结算订单号', width: '120', align: 'left'},
                        {field: 'operatorName', title: '分销商名称', width: '120', align: 'left'},
                        {field: 'stationName', title: '站点', width: '150', align: 'left'},
                        {field: 'orderCount', title: '订单数', width: '80', align: 'right'},
                        {field: 'totalPower', title: '电量', width: '80', align: 'right'},
                        {field: 'totalElecMoney', title: '电费', width: '80', align: 'right'},
                        {field: 'totalSeviceMoney', title: '服务费', width: '80', align: 'right'},
                        {field: 'totalDiscountMoney', title: '优惠金额', width: '80', align: 'right'},
                        {field: 'totalMoney', title: '总金额', width: '80', align: 'right'},
                        {field: 'payAmount', title: '实付金额', width: '80', align: 'right'},
                        {
                            field: '', title: '账单日', width: '120', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.acctDayString!=''&&row.acctDayString!=null){
                                    return row.acctDayString + '号'
                                }
                                return '-';
                            }
                        },
                        {
                            field: '', title: '账单时间', width: '180', align: 'left',
                            formatter: function (value, row, index) {
                                return row.billStartTime + '至' + row.billEndTime
                            }
                        },
                        {field: 'runTime', title: '出账时间', width: '120', align: 'left'},

                        {
                            field: '', title: '结算规则', width: '150', align: 'left',
                            formatter: function (value, row, index) {
                                if ((row.elecAcctRule!=''&&row.elecAcctRule!=null)&&(row.serviceAcctRule!=''&&row.serviceAcctRule!=null)){
                                    return '结算电费=' + row.elecAcctRule + ';结算服务费=' + row.serviceAcctRule
                                }
                                return '-'
                            }
                        },
                        {field: 'acctRuleRemark', title: '规则说明', width: '150', align: 'left',},
                        {field: 'billTotalElecMoney', title: '规则电费', width: '120', align: 'right',},
                        {field: 'billTotalSeviceMoney', title: '规则服务费', width: '120', align: 'right',},
                        {field: 'billTotalMoney', title: '规则总金额', width: '120', align: 'right',},
                        {field: 'adjustElecAmount', title: '调整电费金额', width: '120', align: 'right',},
                        {field: 'adjustServiceAmount', title: '调整服务费金额', width: '120', align: 'right',},
                        {field: 'remark', title: '调整备注', width: '120', align: 'right',},
                        {field: 'elecAcctAmount', title: '结算电费', width: '120', align: 'right',},
                        {field: 'serviceAcctAmount', title: '结算服务费', width: '120', align: 'right',},
                        {field: 'totalAcctAmount', title: '结算总金额', width: '120', align: 'right',},
                        {field: 'rejectRemark', title: '驳回备注', width: '120', align: 'right',},
                        {field: 'supplyRemark', title: '补充账单备注', width: '120', align: 'right',},
                        {
                            field: '', title: '状态', width: '120', align: 'left', class:
                                'operateBefore-td',
                            formatter: function (value, row, index) {
                                if (row.status == '0') {
                                    return '待审核';
                                } else if (row.status == '1') {
                                    return '待确认账单';
                                } else if (row.status == '2') {
                                    return '待复核';
                                } else if (row.status == '3') {
                                    return '结算完成';
                                } else if (row.status== '4'){
                                    return '待推送';
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            width: '200', align: 'center',
                            class: 'operate-td',
                            formatter: function (value, row, index) {
                                var a = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="详情" onclick="acctBillOrder.detailAcctBillOrder(\''
                                    + row.billOrderId
                                    + '\')">详情</a> ';

                                var b = '<a class="btn btn-success btn-sm" href="#" title="订单" onclick="acctBillOrder.orderAcctBillOrder(\''
                                    + row.billOrderId
                                    + '\')">订单</a> ';

                                var c = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="调整" onclick="acctBillOrder.saveReviewAcctBillOrder(\''
                                    + row.billOrderId + '\',\'' + row.billTotalMoney
                                    + '\')">调整</a> ';

                                var d = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="确认账单" onclick="acctBillOrder.acctBank(\''
                                    + row.tenantOperatorCode
                                    + '\')">确认账单</a> ';

                                var e = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="复核" onclick="acctBillOrder.saveReviewAcctBillOrder(\''
                                    + row.billOrderId + '\',\'' + row.billTotalMoney
                                    + '\')">复核</a> ';

                                var f = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="推送" onclick="acctBillOrder.pushAcctBillOrder(\''
                                    + row.billOrderId + '\',\'' + row.adjustElecAmount + '\',\'' + row.adjustServiceAmount + '\',\'' + row.billTotalMoney
                                    + '\')">推送</a> ';

                                var g = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="详情" onclick="acctBillOrder.detailSupplyAcctBillOrder(\''
                                    + row.billOrderId
                                    + '\')">详情</a> ';

                                if (row.status == '0' || row.status == '4') {
                                    if (row.acctRuleId==null||row.acctRuleId==''){
                                        return g + c + f;
                                    }
                                    return a + b + c + f;
                                } else if (row.status == '2') {
                                    if (row.acctRuleId==null||row.acctRuleId==''){
                                        return g + e + f ;
                                    }
                                    return a + b + e + f ;
                                    // } else if (row.status == '4') {
                                    //     if (row.acctRuleId==null||row.acctRuleId==''){
                                    //         return c + f;
                                    //     }
                                    //     return a + b + c + f;
                                } else {
                                    if (row.acctRuleId==null||row.acctRuleId==''){
                                        return g ;
                                    }
                                    return a + b;
                                }
                            }
                        }
                    ],
                    onLoadSuccess: function () {
                        // registCheckbox($("#exampleTable"))
                    }
                });

    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
        // registCheckbox($('#exampleTable'))
        setTimeout(function () {
            $("thead tr").css("height","55px");
            $(".fixed-table-header-columns1").css("width",'189px');
            $(".fixed-table-body-columns1").css("width",'189px');
        },400)
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    function detailAcctBillOrder(billOrderId) {
        location.href = prefix + "/detailAcctBillOrder/" + billOrderId;
    }

    function detailSupplyAcctBillOrder(billOrderId) {
        location.href = prefix + "/detailSupplyAcctBillOrder/" + billOrderId;
    }

    function acctTenantOperatorElecStation(tenantOperatorCode) {
        location.href = prefix + "/acctTenantOperatorElecStation/" + tenantOperatorCode;
    }

    function acctBank(tenantOperatorCode) {
        location.href = prefix + "/acctBank/" + tenantOperatorCode;
    }

    function orderAcctBillOrder(billOrderId) {
        location.href = prefix + "/orderAcctBillOrder/" + billOrderId;
    }

    function closeBtn() {
        $(".auditingModal").addClass("hide");
        $("#adjustElecAmount").val('');
        $("#adjustServiceAmount").val('');
        $("#remark").val('');

    }

    function saveReviewAcctBillOrder(billOrderId, billTotalMoney) {
        $("#billOrderId").val(billOrderId);
        $("#billTotalMoney").val(billTotalMoney)
        $(".auditingModal").removeClass("hide");

    }

    var isajaxsend = true;

    //调整电费及服务费
    $(".addReviewAcctBillOrder").unbind("click").bind("click", function () {
        if ($(this).hasClass("confirmButton")) {
            if (!$("#adjustElecAmount").val()) {
                $("#adjustElecAmount").val("0");
                // layer.msg("请输入调整电费金额");
                // return;
            }
            if (!$("#adjustServiceAmount").val()) {
                $("#adjustServiceAmount").val("0");
                // layer.msg("请输入调整服务费金额");
                // return;
            }
        }
        var formData = new FormData();
        formData.append("adjustElecAmount", $("#adjustElecAmount").val());
        formData.append("adjustServiceAmount", $("#adjustServiceAmount").val());
        formData.append("billTotalMoney", $("#billTotalMoney").val());
        formData.append("remark", $("#remark").val());
        formData.append("billOrderId", $("#billOrderId").val());
        formData.append("status", '4');

        var that = $(this);
        if (isajaxsend) {
            $.ajax({
                url: prefix + "/saveReviewAcctBillOrder",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                beforeSend: function () {
                    isajaxsend = false
                },
                success: function (data) {
                    if (data.status == 200) {
                        isajaxsend = true
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        // $("#adjustPayAmount").val("");
                        $("#adjustElecAmount").val("");
                        $("#adjustServiceAmount").val("");
                        $("#remark").val();
                        $(".auditingModal").addClass("hide");
                        if (that.hasClass("billPage")) {
                            history.back()
                        }
                        reLoad();
                    } else {
                        isajaxsend = true
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        //  $('#exampleModal').modal('hide')
                    }

                }
            })
        }
    })

    //推送账单
    function pushAcctBillOrder(billOrderId,adjustElecAmount,adjustServiceAmount,billTotalMoney) {
        var formData=new FormData();
        formData.append("billOrderId",billOrderId);
        formData.append("adjustElecAmount",adjustElecAmount);
        formData.append("adjustServiceAmount",adjustServiceAmount);
        formData.append("billTotalMoney",billTotalMoney);
        formData.append("status",'1');
        $.ajax({
            url: prefix + "/saveReviewAcctBillOrder",
            type: "post",
            data: formData,
            dataType: "json",
            processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
            contentType: false, // 告诉jQuery不要去设置Content-Type请求头
            beforeSend: function () {
                isajaxsend = false
            },
            success:function (data) {
                if (data.status==200) {
                    layer.msg("推送成功");
                    reLoad();
                }else {
                    layer.alert(data.message);
                }
            }
        })
    }

    function exportAcctBillOrder() {
        $.fileDownload(prefix + "/exportAcctBillOrder", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                tenantOperatorCode: $("#tenantOperatorCode").val().trim(),
                billOrderNum: $("#billOrderNum").val().trim(),
                elecStationId: $("#elecStationId").val().trim(),
                status: $("#status").val().trim(),
                startTime: $("#startTime").val().trim(),
                endTime: $("#endTime").val().trim()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        acctBank: acctBank,
        detailAcctBillOrder: detailAcctBillOrder,
        orderAcctBillOrder: orderAcctBillOrder,
        saveReviewAcctBillOrder: saveReviewAcctBillOrder,
        pushAcctBillOrder:pushAcctBillOrder,
        closeBtn: closeBtn,
        // saveReviewAcctBillOrder: saveReviewAcctBillOrder,
        exportAcctBillOrder: exportAcctBillOrder,
        detailSupplyAcctBillOrder:detailSupplyAcctBillOrder
    };
})();
